package com.javahash.spring.controller;


import com.javahash.spring.service.APIExchangeRates;
import com.javahash.spring.utils.RateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
class ConvertController {
    @Autowired
    APIExchangeRates APIExchangeRates;


    /**
     * Returns a String object that contains JSON data related to the conversion of two currencies.
     *
     * @param value        the amount of currency to be converted
     * @param fromCurrency the currency from which to convert
     * @param toCurrency   the currency to which to convert
     * @return the value of conversion as a String JSON
     */
    @RequestMapping("/convert")
    @ResponseBody
    public Double exchange(

            @RequestParam(value = "amount", required = true) double value,
            @RequestParam(value = "from", required = true) String fromCurrency,
            @RequestParam(value = "to", required = true) String toCurrency) {

        //TODO move logic to service method
        return RateUtils.convertCurrency(fromCurrency, toCurrency, value, APIExchangeRates.getExchangeRates());


    }
}